package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Artical;
import com.ejuornal.bean.Page;
import com.ejuornal.bean.Periodical;
import com.ejuornal.dao.ArticalDao;

@Component("articalDao")
public class ArticalDaoImpl extends DaoSupport<Artical> implements ArticalDao {

	public void add(String sql, Object... objs) {
		super.add(sql, objs);
	}

	public void update(String sql, Object... objs) {
		super.update(sql, objs);
	}

	public void batchDelete(String sql, int... ids) {
		super.batchDelete(sql, ids);
	}

	public Artical findById(int articalId) {
		String sql = "select *  from t_artical where articalId= ?";
		Artical artical = getJdbcTemplate().queryForObject(sql,
				new Object[] { articalId }, new ArticalMapper());

		return artical;
	}

	public List<Artical> findTop(int count) {
		/*String sql = "SELECT * FROM t_artical AS a WHERE a.status=3 ORDER BY viewCount DESC LIMIT 0,?";*/
		String sql = " SELECT a.* FROM t_artical a,t_per_periodical perper,t_periodical per " + 
				" WHERE a.perPeriodicalId = perper.perPeriodicalId AND perper.periodicalId = per.periodicalId " + 
				" AND per.`status` =1 AND a.status=3 ORDER BY viewCount DESC LIMIT 0,? ";
		List<Artical> list = super.getJdbcTemplate().query(sql,
				new Object[] { count }, new ArticalMapper());

		return list;
	}

	public List<Artical> findByBoardId(int boardId, int periodicalId,
			int perPeriodicalId) {
		String sql = "SELECT * FROM t_artical WHERE periodicalId=? AND boardId=? AND perPeriodicalId=?";
		List<Artical> articalList = super.getJdbcTemplate().query(sql,
				new Object[] { periodicalId, boardId }, new ArticalMapper());
		return articalList;
	}

	@Override
	public List<Artical> findByName(String name) {
		String sql = "SELECT * FROM t_artical WHERE status=3 AND title LIKE ? ORDER BY viewCount DESC LIMIT 0, 10";
		List<Artical> articalList = super.getJdbcTemplate().query(sql,
				new Object[] { "%" + name + "%" }, new RowMapper<Artical>() {
					public Artical mapRow(ResultSet rs, int arg1) throws SQLException {
						Artical artical = new Artical();
						artical.setArticalId(rs.getInt("articalId"));
						artical.setTitle(rs.getString("title"));
						artical.setPubDate(rs.getDate("pubDate"));
						
						return artical;
					}
				});
		return articalList;
	}

	public Page<Artical> find(int current_record, String url, String sql_1, String sql_2, Object[] objs) {
		return find(current_record, url, 10, sql_1, sql_2, objs);
	}
	
	public Page<Artical> find(int current_record, String url, int pageSize, String sql_1, String sql_2, Object[] objs) {
		Page<Artical> page = new Page<Artical>();
        @SuppressWarnings("deprecation")

		int totalRecords = super.getJdbcTemplate().queryForInt(sql_1, objs);
		List<Artical> list = (List<Artical>) super.getJdbcTemplate().query(sql_2, objs, new RowMapper<Artical>(){


			@Override
			public Artical mapRow(ResultSet rs, int rowNum) throws SQLException {
				Artical artical = new Artical();
				artical.setArticalId(rs.getInt("articalId"));
				artical.setBoardId(rs.getInt("boardId"));
				artical.setCnAbstract(rs.getString("cnAbstract"));
				artical.setContent(rs.getString("content"));
				artical.setEnAbstract(rs.getString("enAbstract"));
				artical.setKeywords(rs.getString("keywords"));
				artical.setPerPeriodicalId(rs.getInt("perPeriodicalId"));
				artical.setPostDate(rs.getDate("postDate"));
				artical.setPubDate(rs.getDate("pubDate"));
				artical.setRefDoc(rs.getString("refDoc"));
				artical.setStatus(rs.getString("status"));
				artical.setTitle(rs.getString("title"));
				artical.setUserId(rs.getInt("userId"));
				artical.setViewCount(rs.getInt("viewCount"));				
				Periodical periodical = new Periodical();
				periodical.setName(rs.getString("name"));

				artical.setPeriodical(periodical);
				return artical;
			}});
        page.setPageSize(pageSize);
        page.setUrl(url);
        page.setTotalRecords(totalRecords);
        page.setTotalPage(totalRecords);
        page.setRecords(list);
		
		return page;
	}

	public Page<Artical> findArtical(int current_record, String url, int pageSize, String sql_1, String sql_2, Object[] objs) {
		Page<Artical> page = new Page<Artical>();
        @SuppressWarnings("deprecation")

		int totalRecords = super.getJdbcTemplate().queryForInt(sql_1, objs);
		List<Artical> list = (List<Artical>) super.getJdbcTemplate().query(sql_2, objs, new RowMapper<Artical>(){


			@Override
			public Artical mapRow(ResultSet rs, int rowNum) throws SQLException {
				Artical artical = new Artical();
				artical.setArticalId(rs.getInt("articalId"));
				artical.setBoardId(rs.getInt("boardId"));
				artical.setCnAbstract(rs.getString("cnAbstract"));
				artical.setContent(rs.getString("content"));
				artical.setEnAbstract(rs.getString("enAbstract"));
				artical.setKeywords(rs.getString("keywords"));
				artical.setPerPeriodicalId(rs.getInt("perPeriodicalId"));
				artical.setPostDate(rs.getDate("postDate"));
				artical.setPubDate(rs.getDate("pubDate"));
				artical.setRefDoc(rs.getString("refDoc"));
				artical.setStatus(rs.getString("status"));
				artical.setTitle(rs.getString("title"));
				artical.setUserId(rs.getInt("userId"));
				artical.setViewCount(rs.getInt("viewCount"));				
				return artical;
			}});
        page.setPageSize(pageSize);
        page.setUrl(url);
        page.setTotalRecords(totalRecords);
        page.setTotalPage(totalRecords);
        page.setRecords(list);
		
		return page;
	}
    @Override
    public List<Artical> findByPerPeriodical(int id) {
        String sql = "select articalId, title, cnAbstract, enAbstract, content, refDoc from t_artical where perPeriodicalId=?";
        List<Artical> articals = super.getJdbcTemplate().query(sql, 
                new Object[]{id},
                new RowMapper<Artical>(){
                    @Override
                    public Artical mapRow(ResultSet rs, int arg1) throws SQLException {
                        Artical artical = new Artical();
                        artical.setArticalId(rs.getInt("articalId"));
                        artical.setTitle(rs.getString("title"));
                        artical.setCnAbstract(rs.getString("cnAbstract"));
                        artical.setEnAbstract(rs.getString("enAbstract"));
                        artical.setContent(rs.getString("content"));
                        artical.setRefDoc(rs.getString("refDoc"));
                        return artical;
                    }
            
                }
                );
        return articals;
    }
	
}


final class ArticalMapper implements RowMapper<Artical> {

	public Artical mapRow(ResultSet rs, int rowNum) throws SQLException {
		Artical artical = new Artical();
		artical.setArticalId(rs.getInt("articalId"));
		artical.setBoardId(rs.getInt("boardId"));
		artical.setCnAbstract(rs.getString("cnAbstract"));
		artical.setContent(rs.getString("content"));
		artical.setEnAbstract(rs.getString("enAbstract"));
		artical.setKeywords(rs.getString("keywords"));
		artical.setPerPeriodicalId(rs.getInt("perPeriodicalId"));
		artical.setPostDate(rs.getDate("postDate"));
		artical.setPubDate(rs.getDate("pubDate"));
		artical.setRefDoc(rs.getString("refDoc"));
		artical.setStatus(rs.getString("status"));
		artical.setTitle(rs.getString("title"));
		artical.setUserId(rs.getInt("userId"));
		artical.setViewCount(rs.getInt("viewCount"));

		return artical;
	}
	

}
